package com.jk.aspect;

import com.jk.bean.Log;
import com.jk.bean.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.util.Date;
import java.util.Properties;
@Aspect
@Component
public class LogAspect {


    @Autowired
    private MongoTemplate mongoTemplate;


    @Pointcut("execution(* com.jk.service.impl.LoginServiceImpl.userLogin(..))")
     public void pointTcut(){

    }

    @AfterReturning(value = "pointTcut()",returning = "rtv")
    public void afterLogin(JoinPoint joinPoint,Object rtv){

        User user = (User) rtv;

        User user1 = new User();

        if(user!=null){


        if(user.getPhone()!=null&&user.getPhone()!=""){
             user1.setPhone(user.getPhone());
            user1.setUserAccout(user.getUserAccout());
        }else {
            user1.setPhone("未绑定手机号");
            user1.setUserAccout(user.getUserAccout());
        }

        }


        String  name = "";
        String version = "";
        try{
            InetAddress addr = InetAddress.getLocalHost();
            String ip=addr.getHostAddress().toString(); //获取本机ip
           name=addr.getHostName().toString(); //获取本机计算机名称
            System.out.println("本机IP："+ip+"\n本机名称:"+name);
            Properties props=System.getProperties();
            System.out.println("操作系统的名称："+props.getProperty("os.name"));
            System.out.println("操作系统的版本："+props.getProperty("os.version"));
            System.out.println("操作系统的位数："+props.getProperty("os.arch"));
            version = props.getProperty("os.name");
        }catch(Exception e){
            e.printStackTrace();
        }

        Log log = new Log(user1.getUserAccout(),new Date(), name, version, user1.getPhone() );

          mongoTemplate.save(log);
    }



}
